package net.wanho.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.wanho.constant.RoleConstant;
import net.wanho.constant.UserConstant;
import net.wanho.dto.AuthGrantDTO;
import net.wanho.dto.RoleDTO;
import net.wanho.mapper.AuthInfoMapper;
import net.wanho.mapper.RoleAuthMapper;
import net.wanho.mapper.UserInfoMapper;
import net.wanho.po.AuthInfo;
import net.wanho.po.Role;
import net.wanho.po.RoleAuth;
import net.wanho.po.UserInfo;
import net.wanho.service.RoleService;
import net.wanho.mapper.RoleMapper;
import net.wanho.util.CurrentUtil;
import net.wanho.vo.AuthVO;
import net.wanho.vo.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.role;

/**
* @author 王洋
* @description 针对表【role(角色表)】的数据库操作Service实现
* @createDate 2024-09-03 17:43:36
*/
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{
    private final UserInfoMapper userInfoMapper;
    private final AuthInfoMapper authInfoMapper;
    private final RoleAuthMapper roleAuthMapper;

    @Override
    public List<Role> findlist() {

        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getRoleState, RoleConstant.ROLE_STATE);
        return this.list(wrapper);
    }

    @Override
    public PageInfo<Role> listPage(Integer pageNum, Integer pageSize, RoleDTO roleDTO) {
        Page<Role> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!ObjectUtils.isEmpty(roleDTO.getRoleName()), Role::getRoleName,roleDTO.getRoleName());
        wrapper.eq(!ObjectUtils.isEmpty(roleDTO.getRoleCode()), Role::getRoleCode,roleDTO.getRoleCode());
        wrapper.eq(!ObjectUtils.isEmpty(roleDTO.getRoleState()), Role::getRoleState,roleDTO.getRoleState());

        this.page(page, wrapper);
        PageInfo<Role> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page.getCurrent());
        pageInfo.setPageSize(page.getSize());
        pageInfo.setTotalNum(page.getTotal());
        pageInfo.setPageCount(page.getPages());
        pageInfo.setResultList(page.getRecords());
        pageInfo.setLimitIndex((page.getCurrent()-1) * page.getSize());

        page.getRecords().forEach(item -> {
            item.setGetCode(userInfoMapper.selectById(item.getCreateBy()).getUserCode());
        });
        return pageInfo;

    }

    @Override
    public List<Integer> getRoleAuthIdList(Integer roleId) {
        List<Integer> list = authInfoMapper.selectByRoleId(roleId);
        System.out.println(list);
        return list;


    }

    @Override
    public void updateAuth(AuthGrantDTO authGrantDTO) {
        authInfoMapper.deleteByRoleId(authGrantDTO.getRoleId());

        authGrantDTO.getAuthIds().forEach(item -> {
            RoleAuth roleAuth = new RoleAuth();
            roleAuth.setRoleId(authGrantDTO.getRoleId());
            roleAuth.setAuthId(item);
            roleAuthMapper.insert(roleAuth);
        });

    }

}




